package com.lavida.project.system.performance.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lavida.common.exception.job.TaskException;
import com.lavida.common.exception.job.TaskException.Code;
import com.lavida.common.utils.DateUtils;
import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.aspectj.lang.annotation.Log;
import com.lavida.framework.aspectj.lang.enums.BusinessType;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.framework.web.domain.AjaxResult;
import com.lavida.framework.web.page.TableDataInfo;
import com.lavida.project.system.performance.domain.Performance;
import com.lavida.project.system.performance.service.PerformanceService;
import com.lavida.project.system.performance.service.PerformanceServiceImpl;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;
import com.sun.jna.platform.win32.WinUser.MSG;
@Controller
@RequestMapping("/system/performance")
public class PerformanceController extends BaseController{
	private String prefix = "system/performance";
	@Autowired
	private PerformanceService performanceService;
	@Autowired
	private IUserService userService;
	@Autowired
	private PerformanceServiceImpl performanceServiceImpl;
	@RequestMapping()
	public String goods(ModelMap mmap) {
		User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
		return prefix + "/performance";
	}
	/**
	 *    查询绩效
	 * @param performance
	 * @param request
	 * @return
	 */
	@PostMapping("/list")
	@RequiresPermissions("payroll:performance:list")
	@ResponseBody
	public TableDataInfo list(Performance performance, HttpServletRequest request) {
		startPage();
		String dates_from = request.getParameter("fromtime");
		String dates_to = request.getParameter("totime");
		List<Performance> selectPerformance = performanceService.selectPerformance(performance,dates_from,dates_to);
		return getDataTable(selectPerformance);
	}
	/**
	 * 	获取要修改绩效对象
	 * @param id
	 * @return
	 */
	@PostMapping("/getPerformance")
	@ResponseBody
	public Map<String, Object> getPerformance(int id) {
		Map<String, Object> result = new HashMap<String, Object>();
		Performance performance = performanceService.getPerformanceById(id);
		result.put("performance", performance);
		return result;
	}
	
	@PostMapping("/getPerformancByjobId")
	@ResponseBody
	public boolean getPerformancebyId(Performance performance) {
		List<Performance> performances = performanceService.getPerformanceByjobId(performance.getJob_id());
		if(performances!=null) {
			return true;
		}
		return false;
	}
	/**
	 * 	保存修改对象的数据
	 * @param id
	 * @return
	 */
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PostMapping("/updatePerformance")
	@RequiresPermissions("payroll:performance:update")
	@ResponseBody
	public Map<String, Object> updatePerformance(Performance performance) {
		Map<String, Object> result = new HashMap<String,Object>();
		String msg = "";
		String code ="";
		int per = performanceService.updatePerformance(performance);
		if (per==1) {
			msg = "修改成功";
			code = "0";
		}else {
			msg = "修改失败";
			code = "500";
		}
		result.put("msg", msg);
		result.put("code", code);
		return result;
	}
	/**
	 * 	添加绩效
	 * @param performance
	 * @return
	 */
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	@PostMapping("/insertPerformance")
	@RequiresPermissions("payroll:performance:add")
	@ResponseBody
	public Map<String, Object> insertPerformance(Performance performance){
		Map<String, Object> result = new HashMap<String,Object>();
		String msg = "";
		String code ="";
		int per = performanceService.insertPerformances(performance);
		if (per==1) {
			msg = "添加成功";
			code = "0";
		}else {
			msg = "添加失败";
			code = "500";
		}
		result.put("msg", msg);
		result.put("code", code);
		return result;
	}
	/**
	 * 	删除绩效
	 * @param ids
	 * @return
	 */
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@RequiresPermissions("payroll:performance:update")
	@PostMapping("/delete")
	@ResponseBody
	public Map<String, Object> deletePerformance(@RequestParam("ids[]")int[] ids) {
		Map<String, Object> result = new HashMap<String, Object>();
		performanceService.deletePerformance(ids);
		result.put("msg", 1);
		return result;
	}
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PostMapping("/upload")
	@RequiresPermissions("payroll:performance:update")
    @ResponseBody
    public String upload(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws Exception {
		// 获取文件名，包含后缀
	    String fileName = file.getOriginalFilename();
	    // 获取文件后缀
	    String prefix = fileName.substring(fileName.lastIndexOf("."));
	    // 保存文件名
	    String saveFileName = String.valueOf(System.currentTimeMillis()) + prefix;
	    // 保存路径
	    String saveFilePath = "C:\\work\\upload\\";
	    // 保存文件
	    if (file.isEmpty()) {
			return "failed";
		}
	    File saveFile = new File(saveFilePath,saveFileName);
	    if (!saveFile.getParentFile().exists()) {
			saveFile.getParentFile().mkdirs();
			System.out.println("创建目录" + saveFile);
		}
	    System.out.println(saveFile.getPath());
	    file.transferTo(saveFile);
	    performanceService.insertPerformance(saveFile.getPath());
	    return "01";
		
	} 
	
}